{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "import h5py\n",
        "import csv"
      ],
      "metadata": {
        "id": "Ay2mwWMjKs_2"
      },
      "execution_count": 1,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "csv_file_path=\"/content/drive/MyDrive/SoumayaMehrzi/segment_files/usual_features.csv\""
      ],
      "metadata": {
        "id": "B_i0Gd6YN_V3"
      },
      "execution_count": 25,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def cast_list_of_float_to_integer(list_of_float):\n",
        "\n",
        "\n",
        "  list_of_integer = [int(float_value) for float_value in list_of_float]\n",
        "  return list_of_integer\n"
      ],
      "metadata": {
        "id": "t2mz3u-DQlAg"
      },
      "execution_count": 26,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def get_elements_by_indexes_safe(list1, index_list):\n",
        "  new_list = []\n",
        "  for index in index_list:\n",
        "    try:\n",
        "      new_list.append(list1[index])\n",
        "    except IndexError:\n",
        "      return []\n",
        "\n",
        "  return new_list\n"
      ],
      "metadata": {
        "id": "y0qGnrUSQob3"
      },
      "execution_count": 27,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "metadata": {
        "id": "p4Z0sFbsKWQl"
      },
      "outputs": [],
      "source": [
        "def matToCsv(mat_file_path,csv_file_path):\n",
        "# Open the MATLAB v7.3 file using h5py\n",
        "    with h5py.File(mat_file_path, 'r') as file:\n",
        "        # Access the object reference dataset within the group 'Subj_Wins'\n",
        "        ABP_F=file['/Subj_Wins/ABP_F'][:]\n",
        "        var1=list()\n",
        "        for ref_array in ABP_F:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var1.append(dereferenced_data)\n",
        "        ABP_Lag=file['/Subj_Wins/ABP_Lag'][:]\n",
        "        var2=list()\n",
        "        for ref_array in ABP_Lag:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var2.extend(dereferenced_data)\n",
        "        ABP_Raw=file['/Subj_Wins/ABP_Raw'][:]\n",
        "        var3=list()\n",
        "        for ref_array in ABP_Raw:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var3.append(dereferenced_data)\n",
        "        ABP_SPeaks=file['/Subj_Wins/ABP_SPeaks'][:]\n",
        "        var4=list()\n",
        "        for ref_array in ABP_SPeaks:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var4.append(dereferenced_data)\n",
        "        ABP_Turns=file['/Subj_Wins/ABP_Turns'][:]\n",
        "        var5=list()\n",
        "        for ref_array in ABP_Turns:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var5.append(dereferenced_data)\n",
        "        Age=file['/Subj_Wins/Age'][:]\n",
        "        var6=list()\n",
        "        for ref_array in Age:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var6.extend(dereferenced_data)\n",
        "        CaseID=file['/Subj_Wins/CaseID'][:]\n",
        "        var7=list()\n",
        "        for ref_array in CaseID:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var7.extend(dereferenced_data)\n",
        "        ECG_F=file['/Subj_Wins/ECG_F'][:]\n",
        "        var8=list()\n",
        "        for ref_array in ECG_F:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var8.append(dereferenced_data)\n",
        "        ECG_RPeaks=file['/Subj_Wins/ECG_RPeaks'][:]\n",
        "        var9=list()\n",
        "        for ref_array in ECG_RPeaks:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var9.append(dereferenced_data)\n",
        "        ECG_Raw=file['/Subj_Wins/ECG_Raw'][:]\n",
        "        var10=list()\n",
        "        for ref_array in ECG_Raw:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var10.append(dereferenced_data)\n",
        "        Gender=file['/Subj_Wins/Gender'][:]\n",
        "        var11=list()\n",
        "        for ref_array in Gender:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var11.extend(dereferenced_data)\n",
        "        IncludeFlag=file['/Subj_Wins/IncludeFlag'][:]\n",
        "        var12=list()\n",
        "        for ref_array in IncludeFlag:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var12.extend(dereferenced_data)\n",
        "        PPG_ABP_Corr=file['/Subj_Wins/PPG_ABP_Corr'][:]\n",
        "        var13=list()\n",
        "        for ref_array in PPG_ABP_Corr:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var13.extend(dereferenced_data)\n",
        "        PPG_F=file['/Subj_Wins/PPG_F'][:]\n",
        "        var14=list()\n",
        "        for ref_array in PPG_F:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var14.append(dereferenced_data)\n",
        "        PPG_Raw=file['/Subj_Wins/PPG_Raw'][:]\n",
        "        var15=list()\n",
        "        for ref_array in PPG_Raw:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var15.append(dereferenced_data)\n",
        "        PPG_SPeaks=file['/Subj_Wins/PPG_SPeaks'][:]\n",
        "        var16=list()\n",
        "        for ref_array in PPG_SPeaks:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var16.append(dereferenced_data)\n",
        "        PPG_Turns=file['/Subj_Wins/PPG_Turns'][:]\n",
        "        var17=list()\n",
        "        for ref_array in PPG_Turns:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var17.append(dereferenced_data)\n",
        "        SegDBP=file['/Subj_Wins/SegDBP'][:]\n",
        "        var18=list()\n",
        "        for ref_array in SegDBP:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var18.extend(dereferenced_data)\n",
        "        SegSBP=file['/Subj_Wins/SegSBP'][:]\n",
        "        var19=list()\n",
        "        for ref_array in SegSBP:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var19.extend(dereferenced_data)\n",
        "        SegmentID=file['/Subj_Wins/SegmentID'][:]\n",
        "        var20=list()\n",
        "        for ref_array in SegmentID:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var20.extend(dereferenced_data)\n",
        "        SubjectID=file['/Subj_Wins/SubjectID'][:]\n",
        "        var21=list()\n",
        "        for ref_array in SubjectID:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var21.extend(dereferenced_data)\n",
        "        WinID=file['/Subj_Wins/WinID'][:]\n",
        "        var22=list()\n",
        "        for ref_array in WinID:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var22.extend(dereferenced_data)\n",
        "        WinSeqID=file['/Subj_Wins/WinSeqID'][:]\n",
        "        var23=list()\n",
        "        for ref_array in WinSeqID:\n",
        "            for ref in ref_array:\n",
        "                # Dereference the object reference\n",
        "                dereferenced_data = file[ref][0]\n",
        "                var23.extend(dereferenced_data)\n",
        "        csv_rows=list()\n",
        "        for i in range(len(var1)):\n",
        "            row_data=list()\n",
        "            row_data.append(get_elements_by_indexes_safe(var1[i],cast_list_of_float_to_integer(var4[i])))\n",
        "            row_data.append((get_elements_by_indexes_safe(var1[i],cast_list_of_float_to_integer(var5[i]))))\n",
        "            row_data.append(var6[i])\n",
        "            row_data.append(var7[i])\n",
        "            row_data.append((get_elements_by_indexes_safe(var10[i],cast_list_of_float_to_integer(var9[i]))))\n",
        "            row_data.append(var11[i])\n",
        "            row_data.append(var12[i])\n",
        "            row_data.append(get_elements_by_indexes_safe(var14[i],cast_list_of_float_to_integer(var16[i])))\n",
        "            row_data.append(get_elements_by_indexes_safe(var14[i],cast_list_of_float_to_integer(var17[i])))\n",
        "            row_data.append(var18[i])\n",
        "            row_data.append(var19[i])\n",
        "            row_data.append(var20[i])\n",
        "            row_data.append(var21[i])\n",
        "            row_data.append(var22[i])\n",
        "            row_data.append(var23[i])\n",
        "            row_data.append(os.path.basename(mat_file_path))\n",
        "            csv_rows.append(row_data)\n",
        "        with open(csv_file_path, 'a', newline='') as csvfile:\n",
        "            csv_writer = csv.writer(csvfile)\n",
        "            # Write data rows\n",
        "            csv_writer.writerows(csv_rows)"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "def process_folder(folder_path):\n",
        "    for filename in os.listdir(folder_path):\n",
        "        if filename.endswith('.mat'):\n",
        "            mat_file_path = os.path.join(folder_path, filename)\n",
        "            matToCsv(mat_file_path, csv_file_path)"
      ],
      "metadata": {
        "id": "j7M-Uy_fKaig"
      },
      "execution_count": 29,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "process_folder(\"/content/drive/MyDrive/SoumayaMehrzi/segment_files/FolderToWorkIn\")"
      ],
      "metadata": {
        "id": "OlFwVXHhOMnv"
      },
      "execution_count": 30,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "dIqcSW2yOVIu"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}